package com.commute.data;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.LinkedList;

public class StopTime {

	private int tripId;
	private Trip trip;
	private Date arrivalTime = null;
	private Date departureTime = null;
	private int stopId;
	private Stop stop;
	private int stopSequence;
	
	/*
	 * 		arrivalTime and departureTime string must be of form HH:mm:ss
	 */
	StopTime(int tripId, Trip trip, int stopId, Stop stop, int stopSequence, String arrivalTime, String departureTime) {
		SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
		try{
			this.arrivalTime = format.parse(arrivalTime);
			this.departureTime = format.parse(departureTime);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		this.tripId = tripId;
		this.trip = trip;
		this.stopId = stopId;
		this.stop = stop;
		this.stopSequence = stopSequence;
	}
	
	StopTime(int tripId, Trip trip, int stopId, Stop stop, int stopSequence, Date arrivalTime, Date departureTime) {
		this.tripId = tripId;
		this.trip = trip;
		this.arrivalTime = arrivalTime;
		this.departureTime = departureTime;
		this.stopId = stopId;
		this.stop = stop;
		this.stopSequence = stopSequence;
	}
	
	public int tripId(){
		return tripId;
	}
	
	public Trip trip(){
		return trip;
	}
	
	public Date arrivalTime(){
		return arrivalTime;
	}
	
	public Date departureTime(){
		return departureTime;
	}
	
	public int stopId(){
		return stopId;
	}
	
	public Stop stop(){
		return stop;
	}
	
	public int stopSequence(){
		return stopSequence;
	}
	
	public static void sort(LinkedList<StopTime> st){
		Collections.sort(st, new StopTimeComparable());
	}

}

class StopTimeComparable implements Comparator<StopTime>{
	@Override
	public int compare(StopTime st1, StopTime st2) {
		Integer o1 = st1.stopSequence();
		Integer o2 = st2.stopSequence();
		return (o1>o2 ? -1 : (o1==o2 ? 0 : 1));
	}
}
